package cn.rjzjh.tapestry.tams.pages.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tapestry5.util.TextStreamResponse;

import redis.clients.jedis.Jedis;
import cn.rjzjh.commons.util.apiext.RedisClient;
import cn.rjzjh.commons.util.web.EasyUiAssist;
import cn.rjzjh.tapestry.busi.SpringInit;
import cn.rjzjh.tapestry.tams.base.ParentPage;

public class RedisManager extends ParentPage {
	public TextStreamResponse onQuery() {
		String key = request.getParameter("key");
		if (StringUtils.isBlank(key)) {
			return retStream(EasyUiAssist.getJsonForGridEmpty());
		}
		Jedis jedis = RedisClient.getConnection(SpringInit.conf);
		Set<String> keys = jedis.keys(key+"*");
		List<Map<String, String>> retlist = new ArrayList<Map<String, String>>();
		if (CollectionUtils.isNotEmpty(keys)) {
			for (String keyEle : keys) {
				Map<String, String> elemap = new HashMap<String, String>();
				elemap.put("key", keyEle);
				String jsonstr = jedis.get(String.valueOf(keyEle));
				jsonstr=jsonstr.replaceAll("\"", "");
				elemap.put("value", jsonstr);
				retlist.add(elemap);
			}
		}
		String retstr = EasyUiAssist.getJsonForGridAlias(retlist,
				retlist.size());
		RedisClient.returnResource(jedis);
		return retStream(retstr);
	}


	public TextStreamResponse onDel() {
        String key=request.getParameter("key");
        Jedis jedis = RedisClient.getConnection(SpringInit.conf);
        jedis.del(key);
        RedisClient.returnResource(jedis);
        return retSuccInfo();
	}
}
